from fastapi import APIRouter, Path, Query, Cookie, Header
from enum import Enum
from typing import Optional, List
from pydantic import BaseModel, Field
from datetime import date

app03 = APIRouter()


# 路径参数和数字验证

@app03.get('/path/parameters')
def path_params01():
    return {'message': 'This is a messageafgshfhjkhfdfdasdfg'}


@app03.get('/path/{parameters}')
def path_params01(parameters: str):
    return {'message': parameters}


class CityName(str, Enum):
    Beijing = 'Beijing China'
    Shanghai = 'Shanghai China'


@app03.get("/enum/{city}")  # 枚举
async def latest(city: CityName):
    if city == CityName.Shanghai:
        return {'city_name': city, 'confirmed': 1492, 'death': 7}
    if city == CityName.Beijing:
        return {'city_name': city, 'confirmed': 971, 'death': 9}
    return {'city_name': city, 'latest': 'unknown'}


# 通过path parameters传递文件路径
@app03.get('/files/{file_path:path}')
def filepath(file_path: str):
    return f'The file path is {file_path}'


# 限制数字的值
@app03.get('/path_/{num}')
def path_params_validate(
        num: int = Path(..., title="Your number", description="不可描述", ge=2, le=20)
):
    return num


# 查询参数和字符串验证
@app03.get('/query')
def page_limit(page: int = 1, limit: Optional[int] = None):
    if limit:
        return {'page': page, 'limit': limit}
    return {'page': page}


@app03.get('/query/bool/conversion')
def type_conversion(param: bool = False):
    return param


@app03.get('/query/validations')
def query_params_validate(
        valus: str = Query(..., min_length=8, max_length=16, regex='^a'),
        values: List[str] = Query(default=['v1', 'v2'], alias='alias_name')
):
    return values, valus


# 请求体和字段
class CityInfo(BaseModel):
    name: str = Field(..., example='Beijing')
    country: str
    country_code: str = None
    country_population: int = Field(default=800, title="人口数量", description="国家的人口数量", ge=800)

    class Config:
        schema_extra = {
            "example": {
                "name": "Shanghai",
                "country": "China",
                "country_code": "CN",
                "country_population": 1400000000,
            }
        }


@app03.post("/request_body/city")
def city_info(city: CityInfo):
    print(city.name, city.country)
    return city.dict()


# 多参数 混合
def mix_city_info(
        name: str,
        city01: CityInfo,
        city02: CityInfo,
        confirmed: int = Query(ge=0, description="确诊数", default=0),
        death: int = Query(ge=0, description="死亡数", default=0),
):
    if name == "Shanghai":
        return {"Shanghai", {"confirmed": confirmed, 'death': death}}
    return city01.dict(), city02.dict()


# 数据格式嵌套的请求体

class Data(BaseModel):
    city: List[CityInfo] = None  # 嵌套
    date: date
    confirmed: int = Field(ge=0, description="确诊数", default=0)
    deaths: int = Field(ge=0, description="死亡数", default=0)
    recovered: int = Field(ge=0, description="痊愈数", default=0)

@app03.put("/request_body/nested")
def nested_models(data: Data):
    return data


# Cookie 和 Header参数

@app03.get("/cookie") # 效果只能用postman测试
def cookie(cookie_id: Optional[str] = Cookie(None)):
    return {'cookie_id':cookie_id}

@app03.get("/header")
def header(user_agent:Optional[str] = Header(None,convert_underscores=True),x_token: List[str] = Header(None)):
    return {"User-Agent":user_agent,"x-token":x_token}